package com.dtdream.principle.demeter;

import java.util.ArrayList;
import java.util.List;

/**
 * 迪米特原则
 *
 * @Author: yewj
 **/
public class DemeterOne {
    public static void main(String[] args) {
        SchoolManage schoolManage = new SchoolManage();
        schoolManage.printAllEmployee(new CollegeManage());
    }
}

// 学校总部员工
class Employee {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

// 学院员工
class CollegeEmployee {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

// 管理学院员工的管理类
class CollegeManage {
    // 返回学院的所有员工
    public List<CollegeEmployee> getAllEmployee() {
        List<CollegeEmployee> lists = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            CollegeEmployee emp = new CollegeEmployee();
            emp.setId("学院员工id=" + i);
            lists.add(emp);
        }
        return lists;
    }
}

// 学校管理类
class SchoolManage {
    // 返回学校总部的员工
    public List<Employee> getAllEmployee() {
        List<Employee> list = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            Employee emp = new Employee();
            emp.setId("学院总部员工id=" + i);
            list.add(emp);
        }
        return list;
    }

    // 完成输出学校总部和学院总部的员工信息
    void printAllEmployee(CollegeManage sub) {

        /**
         * 1、CollegeEmployee不是SchoolManage的直接朋友
         * 2、CollegeEmployee是以局部变量的形式出现在SchoolManage
         * 3、所以这里违反了迪米特原则
         */

        List<CollegeEmployee> list1 = sub.getAllEmployee();
        System.out.println("-------------学院员工-------------");
        for (CollegeEmployee collegeEmployee : list1) {
            System.out.println(collegeEmployee.getId());
        }

        List<Employee> list2 = this.getAllEmployee();
        System.out.println("-------------学校总部员工-------------");
        for (Employee employee : list2) {
            System.out.println(employee.getId());
        }
    }
}
